package com.jago.springcloud.controller;

import com.jago.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {

    //    public static final String BASE_URL = "http://localhost:8001/";
    // 根据注册中心的服务名访问
    public static final String BASE_URL = "http://SPRINGCLOUD-PROVIDER-DEPT/";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return restTemplate.getForObject(BASE_URL + "dept/get/" + id, Dept.class);
    }

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(BASE_URL + "dept/add", dept, Boolean.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list() {
        System.out.println("请求进来");
        return restTemplate.getForObject(BASE_URL + "dept/list", List.class);
    }

}
